package com.powernode.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.Prod;
import com.powernode.domain.ProdTagReference;
import com.powernode.domain.Sku;
import com.powernode.mapper.ProdMapper;
import com.powernode.mapper.ProdPropMapper;
import com.powernode.mapper.ProdTagReferenceMapper;
import com.powernode.mapper.SkuMapper;
import com.powernode.service.ProdService;
import com.powernode.service.ProdTagReferenceService;
import com.powernode.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod> implements ProdService {


    @Autowired
    private ProdMapper prodMapper;

    @Autowired
    private ProdTagReferenceService prodTagReferenceService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private ProdTagReferenceMapper prodTagReferenceMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveProd(Prod prod) {
        prod.setShopId(1L);
        prod.setSoldNum(0); // 销量
        prod.setCreateTime(new Date());
        prod.setUpdateTime(new Date());
        prod.setPutawayTime(new Date());
        prod.setVersion(0);
        Prod.DeliveryModeVo deliveryModeVo = prod.getDeliveryModeVo();
        prod.setDeliveryMode(JSONObject.toJSONString(deliveryModeVo));
        int insert = prodMapper.insert(prod);
        if(insert > 0) {
            Long prodId = prod.getProdId();
//            处理商品与分组标签的关系
            List<Long> tagList = prod.getTagList();
            if(CollectionUtil.isNotEmpty(tagList) && tagList.size() != 0) {
                List<ProdTagReference> prodTagReferenceList = new ArrayList<>();
                tagList.forEach(tagId->{
//                    创建商品与分组标签的关系记录
                    ProdTagReference prodTagReference = new ProdTagReference();
                    prodTagReference.setTagId(tagId);
                    prodTagReference.setProdId(prodId);
                    prodTagReference.setCreateTime(new Date());
                    prodTagReference.setShopId(1L);
                    prodTagReference.setStatus(1);
                    prodTagReferenceList.add(prodTagReference);
                });
                prodTagReferenceService.saveBatch(prodTagReferenceList);
            }
//            处理商品sku的关系
            List<Sku> skuList = prod.getSkuList();
            if(CollectionUtil.isNotEmpty(skuList) && skuList.size() != 0) {
                skuList.forEach(sku->{
                    sku.setProdId(prodId);
                    sku.setCreateTime(new Date());
                    sku.setUpdateTime(new Date());
                    sku.setVersion(1);
                    sku.setActualStocks(sku.getStocks());
                });
                skuService.saveBatch(skuList);
            }

        }
        return insert > 0;
    }

    @Override
    public Prod queryProdInfoById(Long prodId) {
        Prod prod = prodMapper.selectById(prodId);
//        查询商品与分组标签关系
        List<ProdTagReference> prodTagReferences = prodTagReferenceMapper.selectList(new LambdaQueryWrapper<ProdTagReference>()
                .eq(ProdTagReference::getProdId, prodId));
        if(CollectionUtil.isNotEmpty(prodTagReferences) && prodTagReferences.size() != 0) {
//            从商品与分组标关系中获取标签id
            List<Long> tagIdList = prodTagReferences.stream().map(ProdTagReference::getTagId).collect(Collectors.toList());
            prod.setTagList(tagIdList);
        }
//        根据商品id查询sku
        List<Sku> skus = skuMapper.selectList(new LambdaQueryWrapper<Sku>()
                .eq(Sku::getProdId, prodId));
        prod.setSkuList(skus);
        return prod;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyProdInfo(Prod prod) {
        Long prodId = prod.getProdId();
//        删除商品原来分组标签的关系
        prodTagReferenceMapper.delete(new LambdaQueryWrapper<ProdTagReference>()
                .eq(ProdTagReference::getProdId, prodId)
        );
        List<Long> tagList = prod.getTagList();
        if(CollectionUtil.isNotEmpty(tagList) && tagList.size() != 0) {
            List<ProdTagReference> prodTagReferenceList = new ArrayList<>();
            tagList.forEach(tagId->{
//                    创建商品与分组标签的关系记录
                ProdTagReference prodTagReference = new ProdTagReference();
                prodTagReference.setTagId(tagId);
                prodTagReference.setProdId(prodId);
                prodTagReference.setCreateTime(new Date());
                prodTagReference.setShopId(1L);
                prodTagReference.setStatus(1);
                prodTagReferenceList.add(prodTagReference);
            });
            prodTagReferenceService.saveBatch(prodTagReferenceList);
        }
//        批量修改商品sku
        List<Sku> skuList = prod.getSkuList();
        skuList.forEach(sku->{
            sku.setUpdateTime(new Date());
            sku.setActualStocks(sku.getStocks());
        });
        skuService.updateBatchById(skuList);
        prod.setUpdateTime(new Date());
        return prodMapper.updateById(prod) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeProdById(Long prodId) {
//        删除商品与分组标签的关系
        prodTagReferenceMapper.delete(new LambdaQueryWrapper<ProdTagReference>()
                .eq(ProdTagReference::getProdId, prodId)
        );
//        根据商品id删除sku
        skuMapper.delete(new LambdaQueryWrapper<Sku>()
                .eq(Sku::getProdId, prodId)
        );
        return prodMapper.deleteById(prodId) > 0;
    }
}
